package com.shanjupay.uaa.domain;

import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IntegrationUserDetailsAuthenticationHandler {

    /**
     * 认证处理 简易判断，后期优化结构
     * @param domain 用户域 ，如b端用户、c端用户等
     * @param authenticationType  认证类型，如密码认证，短信认证等
     * @param token
     * @return
     */
    public UnifiedUserDetails authentication(String domain, String authenticationType,  UsernamePasswordAuthenticationToken token){


        UnifiedUserDetails userDetails = null;

        if("c".equals(domain)){ //c端用户


        }else if("b".equals(domain)){ // b端用户

            if("password".equals(authenticationType)){ // 密码认证

                String username = (token.getPrincipal() == null) ? "NONE_PROVIDED"
                        : token.getName();

                if (username == null) {
                    throw new BadCredentialsException("username is blank");
                }

                if (token.getCredentials() == null) {
                    throw new BadCredentialsException("Credentials is blank");
                }

                String presentedPassword = token.getCredentials().toString();

               /* UserBAgent userBAgent = (UserBAgent)ApplicationContextHelper.getBean(UserBAgent.class);
                UserBDTO dto = new UserBDTO();
                dto.setUserName(username);
                dto.setPassword(presentedPassword);
                RestResponse<UserBDTO> rst = userBAgent.loginByPassword(dto);
                if(rst.getCode() == -2){
                    throw new BadCredentialsException("用户名或者密码错误");
                }
                if(rst.getCode() == CommonErrorCode.E_999992.getCode()){
                    throw new BadCredentialsException("用户服务访问异常");
                }
                if(rst.getCode() != 0){
                    throw new BadCredentialsException("认证错误");
                }
                userDetails = new UnifiedUserDetails(rst.getResult().getUserName(),presentedPassword,AuthorityUtils.createAuthorityList("DEFAULT_ANONYMOUS"));
                userDetails.setTenantId(rst.getResult().getClientLicenseId());*/



               userDetails =  getUserDetails(username);

                if(userDetails == null){
                    throw new BadCredentialsException("User not found");
                }

                if (!NoOpPasswordEncoder.getInstance().matches(presentedPassword, userDetails.getPassword())) {
                    throw new BadCredentialsException("Password authentication failed");
                }
            }

        }

        if(userDetails == null){
            throw new BadCredentialsException("User not found");
        }



        return  userDetails;
    }

    private UnifiedUserDetails getUserDetails(String username) {
        Map<String ,UnifiedUserDetails> userDetailsMap = new HashMap<>();
        userDetailsMap.put("admin", new UnifiedUserDetails ("admin","111111",AuthorityUtils.createAuthorityList("ROLE_PAGE_A","PAGE_B")));
        userDetailsMap.put("xufan", new UnifiedUserDetails ("xufan","111111",AuthorityUtils.createAuthorityList("ROLE_PAGE_A","PAGE_B")));

        userDetailsMap.get("admin").setDepartmentId("1");
        userDetailsMap.get("admin").setMobile("18611106983");
        userDetailsMap.get("admin").setTenantId("1");
        Map<String, List<String>> au1 = new HashMap<>();
        au1.put("ROLE1",new ArrayList<>());
        au1.get("ROLE1").add("p1");
        au1.get("ROLE1").add("p2");
        userDetailsMap.get("admin").setUserAuthorities(au1);
        Map<String, Object> payload1 = new HashMap<>();
        payload1.put("res","res1111111");
        userDetailsMap.get("admin").setPayload(payload1);


        userDetailsMap.get("xufan").setDepartmentId("2");
        userDetailsMap.get("xufan").setMobile("18611106984");
        userDetailsMap.get("xufan").setTenantId("1");
        Map<String, List<String>> au2 = new HashMap<>();
        au2.put("ROLE2",new ArrayList<>());
        au2.get("ROLE2").add("p3");
        au2.get("ROLE2").add("p4");
        userDetailsMap.get("xufan").setUserAuthorities(au2);

        Map<String, Object> payload2 = new HashMap<>();
        payload2.put("res","res222222");
        userDetailsMap.get("xufan").setPayload(payload2);

        return userDetailsMap.get(username);

    }

}
